<?php
error_reporting(0);
define('SYSTEM_ROOT', dirname(__FILE__).'/');
define('ROOT', dirname(SYSTEM_ROOT).'/');
function curl_get($url)
{
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Linux; U; Android 4.4.1; zh-cn; R815T Build/JOP40D) AppleWebKit/533.1 (KHTML, like Gecko)Version/4.0 MQQBrowser/4.5 Mobile Safari/533.1');
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    $content = curl_exec($ch);
    curl_close($ch);
    return ($content);
}

function daddslashes($string, $force = 0, $strip = FALSE)
{
    !defined('MAGIC_QUOTES_GPC') && define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
    if (!MAGIC_QUOTES_GPC || $force) {
        if (is_array($string)) {
            foreach ($string as $key => $val) {
                $string[$key] = daddslashes($val, $force, $strip);
            }
        } else {
            $string = addslashes($strip ? stripslashes($string) : $string);
        }
    }
    return $string;
}

function send_template($data)
{
    global $site_row, $siteurl;
    return "<div id=\"cTMail-Wrap\" style=\"box-sizing:border-box;text-align:center;min-width:320px; max-width:660px; border:1px solid #f6f6f6; background-color:#f7f8fa; margin:auto; padding:20px 0 30px; font-family:&#39;helvetica neue&#39;,PingFangSC-Light,arial,&#39;hiragino sans gb&#39;,&#39;microsoft yahei ui&#39;,&#39;microsoft yahei&#39;,simsun,sans-serif\">
    <div class=\"main-content\" style=\"\">
        <table style=\"width:100%;font-weight:300;margin-bottom:10px;border-collapse:collapse\">
            <tbody>
            <tr style=\"font-weight:300\">
                <td style=\"width:3%;max-width:30px;\"></td>
                <td style=\"max-width:600px;\">
                    <p style=\"height:2px;background-color: #00a4ff;border: 0;font-size:0;padding:0;width:100%;margin-top:20px;\"></p>
                    <div id=\"cTMail-inner\" style=\"background-color:#fff; padding:23px 0 20px;box-shadow: 0px 1px 1px 0px rgba(122, 55, 55, 0.2);text-align:left;\">
                        <table style=\"width:100%;font-weight:300;margin-bottom:10px;border-collapse:collapse;text-align:left;\">
                            <tbody>
                            <tr style=\"font-weight:300\">
                                <td style=\"width:3.2%;max-width:30px;\"></td>
                                <td style=\"max-width:480px;text-align:left;\">
                                    <h1 id=\"cTMail-title\" style=\"font-weight:bold;font-size:20px; line-height:36px; margin:0 0 16px;\">" . $site_row['webname'] . "-邮件提醒</h1>
                                    <p id=\"cTMail-userName\" style=\"font-size:14px;color:#333; line-height:24px; margin:0;\">尊敬的：" . $data['nickname'] . "，您好！</p>
                                    <p class=\"cTMail-content\" style=\"font-size: 14px; color: rgb(51, 51, 51); line-height: 24px; margin: 6px 0px 0px; word-wrap: break-word; word-break: break-all;\">这封信是由" . $site_row['webname'] . "（" . $_SERVER['HTTP_HOST'] . "）发送的。</p>
                                    <p class=\"cTMail-content\" style=\"font-size: 14px; color: rgb(51, 51, 51); line-height: 24px; margin: 6px 0px 0px; word-wrap: break-word; word-break: break-all;\">您在我们网站挂机的QQ：" . $data['qq'] . " 挂机状态已失效，请及时更新，<a href='" . $siteurl . "'>点我前往更新</a></p>
                                    <p class=\"cTMail-content\" style=\"font-size: 14px; color: rgb(51, 51, 51); line-height: 24px; margin: 6px 0px 0px; word-wrap: break-word; word-break: break-all;\">失效时间：" . date('Y-m-d H:i:s') . ",</p>
                                   <br/>
                                    </p>
                                    <dl style=\"font-size: 14px; color: rgb(51, 51, 51); line-height: 18px;\">
                                        <dd style=\"margin: 0px 0px 6px; padding: 0px; font-size: 12px; line-height: 22px;\"><p id=\"cTMail-sender\" style=\"font-size: 14px; line-height: 26px; word-wrap: break-word; word-break: break-all; margin-top: 32px;\">此致 <br  />
                                            <strong>" . $site_row['webname'] . "</strong></p>
                                        </dd>
                                    </dl>
                                </td>
                                <td style=\"width:3.2%;max-width:30px;\"></td>
                            </tr>
                            </tbody>
                        </table>
                    </div>
                </td>
            </tr>
            </tbody>
        </table>
    </div>
</div>";
}

function get_curl($url, $post = 0, $referer = 1, $cookie = 0, $header = 0, $ua = 0, $nobaody = 0)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    $httpheader[] = "Accept:application/json";
    $httpheader[] = "Accept-Encoding:gzip,deflate,sdch";
    $httpheader[] = "Accept-Language:zh-CN,zh;q=0.8";
    $httpheader[] = "Connection:close";
    curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
    if ($post) {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    }
    if ($header) {
        curl_setopt($ch, CURLOPT_HEADER, TRUE);
    }
    if ($cookie) {
        curl_setopt($ch, CURLOPT_COOKIE, $cookie);
    }
    if ($referer) {
        if ($referer == 1) {
            curl_setopt($ch, CURLOPT_REFERER, 'http://m.qzone.com/infocenter?g_f=');
        } else {
            curl_setopt($ch, CURLOPT_REFERER, $referer);
        }
    }
    if ($ua) {
        curl_setopt($ch, CURLOPT_USERAGENT, $ua);
    } else {
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Linux; U; Android 4.4.1; zh-cn) AppleWebKit/533.1 (KHTML, like Gecko)Version/4.0 MQQBrowser/5.5 Mobile Safari/533.1');
    }
    if ($nobaody) {
        curl_setopt($ch, CURLOPT_NOBODY, 1);
    }
    curl_setopt($ch, CURLOPT_ENCODING, "gzip");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $ret = curl_exec($ch);
    curl_close($ch);
    return $ret;
}

function send_post($url, $post_data)
{

    $postdata = http_build_query($post_data);
    $options = array(
        'http' => array(
            'method' => 'POST',//注意要大写
            'header' => 'Content-type:application/x-www-form-urlencoded',
            'content' => $postdata
        )
    );
    $context = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
    return $result;
}

function send_mail($to, $sub, $msg)
{
    global $site_row, $configs;
    if ($configs['is_mail_api'] == 0) {
        $mail_api = 'http://api.emapi.cn/api/mzemail/send.php';
        $post['to'] = $to;
        $post['title'] = $sub;
        $post['html'] = $msg;
        $post['nick'] = $site_row['webname'];
        $data = send_post($mail_api, $post);
        $res = json_decode($data, true);
        if ($res['code'] == 1000) {
            return true;
        } else {
            return false;
        }
    } elseif ($configs['is_mail_api'] == 1) {
        include 'smtp.class.php';
        $From = $configs['mail_name'];
        $Host = $configs['mail_smtp'];
        $Port = $configs['mail_port'];
        $SMTPAuth = 1;
        $Username = $configs['mail_name'];
        $Password = $configs['mail_pwd'];
        $Nickname = $site_row['webname'];
        $SSL = $configs['mail_port'] == 465 ? 1 : 0;
        $mail = new SMTP($Host, $Port, $SMTPAuth, $Username, $Password, $SSL);
        $mail->att = array();
        if ($mail->send($to, $From, $sub, $msg, $Nickname)) {
            return true;
        } else {
            return false;
        }
    }
}

function skeyzt($uin)
{
    global $pdo, $configs, $site_row;
    $qq = $pdo->query("select * from `zy_qqs` where `qq` = '{$uin}' limit 1")->fetch();
    $sql = "update `zy_qqs` set `cookiezt` =1 where `qid`='{$qq['qid']}'";
    $sql2 = "update `zy_qqjob` set `cookiezt`=1 where `qid`='{$qq['qid']}'";
    $pdo->exec($sql);
    $pdo->exec($sql2);
    if ($configs['is_nqqzt_msg'] == 1) {
        $myrow = $pdo->query("select * from `zy_users` where `uid`='{$qq['uid']}' limit 1")->fetch();
        $sub = $site_row['webname'] . '- QQ失效提醒';
        $data = [
            'nickname' => $myrow['nickname'],
            'qq' => $qq['qq'],
        ];
        $msg = send_template($data);
        send_mail($myrow['mail'], $sub, $msg);
    }
}

function taskRunCache($key)
{
    global $siteurl;
    $is_run = file_get_contents($siteurl . 'index/taskCache?key=' . $key);
    return $is_run;
}

function unique_array($array, $total, $unique = true)
{
    $newArray = array();
    if ((bool)$unique) {
        $array = array_unique($array);
    }
    shuffle($array);
    $length = count($array);
    for ($i = 0; $i < $total; $i++) {
        if ($i < $length) {
            $newArray[] = $array[$i];
        }
    }
    return $newArray;
}


@set_time_limit(0);
ignore_user_abort(true);
header("content-Type: text/html; charset=utf-8");
date_default_timezone_set('PRC');
$mysql = require_once __DIR__ . '../../../App/index/database.php';
try {
    $pdo = new PDO("mysql:host={$mysql['hostname']};dbname={$mysql['database']};port=3306", $mysql['username'], $mysql['password']);
} catch (Exception $e) {
    exit('链接数据库失败:' . $e->getMessage());
}
$pdo->exec("set names utf8");
$stmt = $pdo->query("select * from zy_configs");
while ($Row = $stmt->fetch()) {
    $configs[$Row['k']] = $Row['v'];
}
$site_domain = $_SERVER['HTTP_HOST'];
$site = $pdo->
query(
    "select * from zy_weblist where `domain` = '{$site_domain}' or `domain2` = '{$site_domain}' limit 1"
);
$site_row = $site->fetch();
define('RUN_KEY', md5($mysql['username'] . md5($mysql['password'])));
$date = date("Y-m-d H:i:s");
$scriptpath = str_replace('\\', '/', $_SERVER['SCRIPT_NAME']);
$sitepath = substr($scriptpath, 0, strrpos($scriptpath, '/qq/'));
$siteurl = ($_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $sitepath . '/';
$referer = substr($_SERVER['HTTP_REFERER'], 0, strrpos($_SERVER['HTTP_REFERER'], '/')) . '/';
if (!isset($_GET['runkey'])) $isdisplay = true;
if ($siteurl != $referer) {
    if ($_GET['runkey'] != md5(RUN_KEY)) {
        /* exit('没有访问权'); */
    }
}